//
//  CompanyIntroductionViewController.m
//  CPICMobileServiceApp
//
//  Created by lizhiwei on 13-2-1.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "CompanyIntroductionViewController.h"
#import "AppDelegate.h"
#import "Config.h"
#import "RCAlertViewUtil.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "ASIHTTPRequest.h"
#import "UIImageView+WebCache.h"
#import "NSString+ThreeDES.h"

#define BUTTON_WIDTH 54.0
#define BUTTON_SEGMENT_WIDTH 51.0
#define CAP_WIDTH 5.0

//typedef enum {
//    CapLeft          = 0,
//    CapMiddle        = 1,
//    CapRight         = 2,
//    CapLeftAndRight  = 3
//} CapLocation;
//
//@interface CompanyIntroductionViewController (PrivateMethods)
//-(UIButton*)woodButtonWithText:(NSString*)buttonText stretch:(CapLocation)location;
//-(UIBarButtonItem*)woodBarButtonItemWithText:(NSString*)buttonText;
//@end

@interface CompanyIntroductionViewController ()

@end

@implementation CompanyIntroductionViewController
@synthesize CompanyImageView;
@synthesize progress;

- (id) init
{
    self = [super init];
    if (self) 
    {
        self.title = @"公司介绍";
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
        self.navigationController.navigationBarHidden = FALSE;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    //添加返回按钮
    /*UIButton * Backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    Backbtn.frame = CGRectMake(15, 5, 80, 15);
    [Backbtn addTarget:self action:@selector(clickBackButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Backbtn];*/
    
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
        ADD_LEFT_BUTTONITEM(@"back.png");
        ADD_TITLE_NAMEITEM(@"公司介绍");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88_01@2x.png");
        ADD_LEFT_BUTTONITEM(@"back_01.png");
        ADD_TITLE_NAMEITEM(@"公司介绍");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773_01@2x"]];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        NSLog(@"-------找到%@",dataImg_02);
        NSData *dataImg_03=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_03];
        NSData *dataImg_04=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_04];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
        NSURL *urlSkinImg_03=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_03]];
        NSURL *urlSkinImg_04=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_04]];
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        UIImageView * LogoImage = [[UIImageView alloc]init];
        [LogoImage setImageWithURL:urlSkinImg_03 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_03];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        LogoImage.image = [UIImage imageWithData:dataImg_03];
        LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, LogoImage.image.size.height/2);
        [self.view addSubview:LogoImage];
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
        ADD_TITLE_NAMEITEM(@"公司介绍");
        
        UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,44, self.view.frame.size.width, self.view.frame.size.height-44)];
        [bgimage setImageWithURL:urlSkinImg_04 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(bgimage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_04];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [bgimage setImage:[UIImage imageWithData:dataImg_04]];
        [self.view addSubview:bgimage];
        [bgimage release];
    }
    
    
    
    
    NSString *companyImgVersion=[[NSUserDefaults standardUserDefaults]objectForKey:COMPANY_IMG_VERSION];
    if(companyImgVersion!=nil)
    {
        companyImgVersion=[companyImgVersion substringFromIndex:1];
        ImgVersion=[companyImgVersion floatValue];
    }
    else
    {
        ImgVersion=0.0000;
    }
    //添加文本及内容
//    UITextView * Intro = [[UITextView alloc]initWithFrame:CGRectMake(10, 10+44, 300, 350)];
//    [Intro setBackgroundColor:[UIColor clearColor]];
//    [Intro setText:@"       中国科学院软件研究所实施的知识创新试点工程多项成果荣获国家科技进步奖国内一流大型专业软件开发商、系统集成商在软件开发、系统集成领域涵盖国内众多行业注册资金11250万元，总部设在北京。地址：北京市海淀区中关村中科祥园甲6号；公司现有员工超过5000人，已形成以硕士、博士为骨干的技术、专家队伍上海、广州、深圳设有子公司；以软件开发和信息技术服务为发展基础，提供行业通用应用系统解决方案布局内重点软件企业智能化专项工程设计甲级证书综合布线工程施工资质安全技术防范工程设计、施工准许证电子政务、保险核心业务、办公自动化、林业电子办证等产品认定证书、著作权登记证书 获得国家级奖励和证书12个，省部级奖励证书10个"];
//    [Intro setScrollsToTop:TRUE];
    //[self.view addSubview:Intro];
    
    NSData *dataImg=[[NSUserDefaults standardUserDefaults]objectForKey:COMPANY_IMG_DATA];
    CompanyImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44)];
    CompanyImageView.image=[UIImage imageWithData:dataImg];
    [self.view addSubview:CompanyImageView];
    [self setRequest];
}

-(void) ClickCallOutBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setRequest//向太保移动平台发送请求报文
{
//    NSString *strRequestCode=@"RequestCode=30101";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    [dicRequest setObject:@"ppp" forKey:@"userAccount"];
    NSString *strRequestJson=[dicRequest JSONRepresentation];
    //strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request setPostValue:@"30101" forKey:@"RequestCode"];
    [request setPostValue:strRequestJson forKey:@"RequestData"];
    
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseFail:)];
    [request setDidFinishSelector:@selector(ResonseSuccess:)];
    [request startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        //[NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    };
//    NSString *strEn=@"测试{id:'李道刚',name:'lidaogang',email:'795370331@qq.com'}";
//    NSLog(@"加密前的报文为:%@",strEn);
//    NSString *strEncrypt=[NSString encrypt:strEn];
//    NSLog(@"加密后的报文为:%@",strEncrypt);
//    NSString *strDecrpt=[NSString decrypt:strEncrypt];
//    NSLog(@"解密后的报文为:%@",strDecrpt);
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    float ImgServelVersion=0;
    NSString *strImgServelVersion=@"0000";
    if([dictResponse valueForKey:@"version"])
    {
        strImgServelVersion=[dictResponse valueForKey:@"version"];
        ImgServelVersion=[[dictResponse valueForKey:@"version"] floatValue];
        NSString *strVersion=[dictResponse valueForKey:@"version"];
        strVersion=[strVersion substringFromIndex:1];
        ImgServelVersion=[strVersion floatValue];
        NSLog(@"ImgServelVersion %f",ImgServelVersion);
        NSLog(@"ImgVersion %f",ImgVersion);
    }
    if(ImgServelVersion!=ImgVersion)
    {
        [[NSUserDefaults standardUserDefaults] setObject:strImgServelVersion forKey:COMPANY_IMG_VERSION];
        ImgUrl=[dictResponse valueForKey:@"imgUrl"];
        ImgUrl=[NSString stringWithFormat:@"%@%@",Image_Url,ImgUrl];
        ImgUrl = [ImgUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
        NSURL *urlCompanyImg=[NSURL URLWithString:ImgUrl];
        [CompanyImageView setImageWithURL:urlCompanyImg success:^(UIImage *image) {
             imgComData=UIImagePNGRepresentation(CompanyImageView.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgComData forKey:COMPANY_IMG_DATA];
        } failure:^(NSError *error) {
        }];

    }
    
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc
{
    [super dealloc];
}
@end
